package com.easychat.controller;

import com.easychat.entity.enums.AppUpdateStatusEnum;
import com.easychat.entity.enums.ResponseCodeEnum;
import com.easychat.entity.po.AppUpdate;
import com.easychat.entity.query.AppUpdateQuery;
import com.easychat.entity.vo.PaginationResultVO;
import com.easychat.entity.vo.ResponseVO;
import com.easychat.exception.BusinessException;
import com.easychat.service.AppUpdateService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;

@RestController("adminAppUpdateController")
@RequestMapping("/admin")
public class AdminAppUpdateController extends ABaseController{

    @Resource
    private AppUpdateService appUpdateService;

    /**
     * 获取更新列表
     * @param appUpdateQuery
     * @return
     */
    @RequestMapping("/loadUpdateList")
    public ResponseVO loadUpdateList(AppUpdateQuery appUpdateQuery) {
        appUpdateQuery.setOrderBy("id desc");
        PaginationResultVO<AppUpdate> pageResult = appUpdateService.findListByPage(appUpdateQuery);
        return getSuccessResponseVO(pageResult);
    }

    /**
     * 保存更新(新增或修改)
     * @param id 不为空表示修改操作
     * @param version 版本号
     * @param updateDesc 更新内容
     * @param fileType 文件类型
     * @param outerLink 外链地址
     * @param file 文件
     * @return
     */
    @RequestMapping("/saveUpdate")
    public ResponseVO saveUpdate(
        Integer id,
        @NotEmpty
        String version,
        @NotEmpty
        String updateDesc,
        @NotNull
        Integer fileType,
        String outerLink,
        MultipartFile file
    ) throws IOException {
        AppUpdate appUpdate = new AppUpdate();
        appUpdate.setId(id);
        appUpdate.setVersion(version);
        appUpdate.setUpdateDesc(updateDesc);
        appUpdate.setFileType(fileType);
        appUpdate.setOuterLink(outerLink);
        appUpdateService.saveUpdate(appUpdate, file);
        return getSuccessResponseVO(null);
    }

    /**
     * 删除更新
     * @param id
     * @return
     */
    @RequestMapping("/delUpdate")
    public ResponseVO delUpdate(@NotNull Integer id) {
        appUpdateService.deleteAppUpdateById(id);
        return getSuccessResponseVO(null);
    }

    /**
     * 发布更新
     * @param id
     * @param status 状态 1 灰度发布 2 全网发布
     * @param grayscaleUid 灰度uid
     * @return
     */
    @RequestMapping("/postUpdate")
    public ResponseVO postUpdate(@NotNull Integer id, @NotNull Integer status, String grayscaleUid) {
        appUpdateService.postUpdate(id, status, grayscaleUid);
        return getSuccessResponseVO(null);
    }
}
